package gg.com.toolbox.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import gg.com.toolbox.util.AppConstant;
import gg.com.toolbox.util.DateUtils;

/**
 * 用户响应服务
 * Created by gaoge on 2017/10/10.
 */

public class LoginService extends Service {
    //记录需要保存的数据
    private int info;
    //对外提供数据的方法
    private MyBinder myBinder = new MyBinder();
    //控制线程
    private boolean lean=true;
    //未响应时长(分钟)
    private int number=10;

    /**
     * 重新绑定时调用该方法
     */
    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    /**
     * 解除绑定时调用该方法
     */
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (lean){
                        Thread.sleep(1000);
                        if(DateUtils.getDateLong_yMdHms()-AppConstant.time>number*60*1000){
                            lean=false;
                            loginHandler.sendEmptyMessage(1);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public class MyBinder extends Binder {
        public Integer getInfo() {
            return info;
        }
    }

    Handler loginHandler = new Handler() {
        @SuppressLint("NewApi")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    info=1;
                    break;
            }
            //发送广播
            Intent intent=new Intent();
            intent.putExtra("type", info);
            intent.setAction("gg.com.toolbox.service.LoginService");
            sendBroadcast(intent);
        }
    };

}
